package wireless.frequent.dao;

import java.util.Iterator;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import wireless.frequent.pojo.Frequent;

import cm.util.dao.BaseHibernateDAO;
import cm.util.hibernate.HibernateSessionFactory;

public class GetFrequentList{

    private String hql;
    private Query query;
    
	public Session getSession() {
		return HibernateSessionFactory.getSession();

	}
	public List<Frequent>  getFreList(){
		try {
		hql = "from Frequent as f where f.length=4";
		query = getSession().createQuery(hql);
		return query.list();
		} catch (RuntimeException re) {
			throw re;
		}
	}
/**	
 * 批量存入数据库
 * @param list
 * @return
 */
	public String saveFrequentList(List<Frequent> list){
		Session session = getSession();
		Transaction tx = session.beginTransaction();
		 
		try {
			//hql = "";
			//query = getSession().createQuery(hql);
			for(Iterator sit=list.iterator();sit.hasNext();)//迭代取值
		     {
				 Frequent frequent=(Frequent)sit.next();
				 session.save(frequent);
		     }	
			 session.flush();
             session.clear();
             tx.commit();
			return "success";
		} catch (RuntimeException re) {
			// TODO: handle exception
			re.printStackTrace();
            tx.rollback();
            throw re;
		
		}finally{
            session.close();
        }
	}
}
